package com.zjmzxfzhl.modules.app.controller;

import java.util.Arrays;

import javax.validation.Valid;

import com.zjmzxfzhl.modules.app.form.AppGitVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjmzxfzhl.common.core.Result;
import com.zjmzxfzhl.common.core.base.BaseController;
import com.zjmzxfzhl.modules.app.entity.AppGit;
import com.zjmzxfzhl.modules.app.service.AppGitService;

/**
 * Controller
 * 
 * @author 张森荣
 */
@RestController
@RequestMapping("/app/git")
public class AppGitController extends BaseController {
    @Autowired
    private AppGitService appGitService;

    /**
     * 自定义查询列表
     * 
     * @param appGitVo
     * @param current
     * @param size
     * @return
     */
//    @PreAuthorize("@elp.single('app:git:list')")
    @GetMapping(value = "/list")
    public Result list(AppGitVo appGitVo, @RequestParam Integer current, @RequestParam Integer size) {

        IPage<AppGit> pageList = appGitService.list(new Page<AppGit>(current, size), appGitVo);
        return Result.ok(pageList);
    }

//    @PreAuthorize("@elp.single('app:git:list')")
    @GetMapping(value = "/queryById")
    public Result queryById(@RequestParam String id) {
        AppGit appGit = appGitService.getById(id);
        return Result.ok(appGit);
    }

    /**
     * @功能：新增
     * @param appGit
     * @return
     */
//    @PreAuthorize("@elp.single('app:git:save')")
    @PostMapping(value = "/save")
    public Result save(@Valid @RequestBody AppGit appGit, @RequestParam String projId) {
        AppGit result=appGitService.saveAppGit(appGit,projId);
        if(result==null){
            return Result.error("仓库创建失败！");
        }
        return Result.ok(result);
    }

    /**
     * @功能：修改
     * @param appGit
     * @return
     */
//    @PreAuthorize("@elp.single('app:git:update')")
    @PutMapping(value = "/update")
    public Result update(@Valid @RequestBody AppGit appGit) {
        appGitService.updateById(appGit);
        return Result.ok();
    }

    /**
     * @功能：批量删除
     * @param ids
     * @return
     */
//    @PreAuthorize("@elp.single('app:git:delete')")
    @DeleteMapping(value = "/delete")
    public Result delete(@RequestParam String ids) {
        if (ids == null || ids.trim().length() == 0) {
            return Result.error("ids can't be empty");
        }
        String[] idsArr = ids.split(",");
        if (idsArr.length > 1) {
            appGitService.removeByIds(Arrays.asList(idsArr));
        } else {
            appGitService.removeById(idsArr[0]);
        }
        return Result.ok();
    }
}
